import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome" */ '../components/Welcome.vue')

const User = () => import(/* webpackChunkName: "User_Role_Right" */ '../components/user/User.vue')
const Role = () => import(/* webpackChunkName: "User_Role_Right" */ '../components/rights/Role.vue')
const Right = () => import(/* webpackChunkName: "User_Role_Right" */ '../components/rights/Right.vue')

const Good = () => import(/* webpackChunkName: "Good_Add_Cate" */ '../components/goods/Good.vue')
const Add = () => import(/* webpackChunkName: "Good_Add_Cate" */ '../components/goods/Add.vue')
const Cate = () => import(/* webpackChunkName: "Good_Add_Cate" */ '../components/goods/Cate.vue')

const Report = () => import(/* webpackChunkName: "Report_Business_Merchant" */ '../components/report/Report.vue')
const Business = () => import(/* webpackChunkName: "Report_Business_Merchant" */ '../components/report/Business.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/login', name: 'Login', component: Login },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/home', name: 'welcome', component: Welcome },
      { path: '/users', name: 'User', component: User },
      { path: '/goods', name: 'Good', component: Good },
      { path: '/roles', name: 'Role', component: Role },
      { path: '/rights', name: 'Right', component: Right },
      { path: '/add', name: 'AddGoods', component: Add },
      { path: '/cates', name: 'Category', component: Cate },
      { path: '/reports', name: 'Report', component: Report },
      { path: '/business', name: 'Business', component: Business }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径访问
  // next 放行  next(指定路径) 强制跳转到指定路径页面
  if (to.path === '/login') return next()
  const user = window.sessionStorage.getItem('user')
  if (!user) return next('/login')
  next()
})

export default router
